﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using space_matters.Utilities;

namespace space_matters
{
    class Star : Matter
    {
        #region Private Elements

        private TimeSpan _life;
        private Boolean animEnded = false;

        private bool _beganExplosion;

        #endregion

        public bool IsCompletelyDead { get; set; }

        #region Constructors

        public Star(TimeSpan life, Color color, double mass, double radius, Vector2 position)
            : this(life, color, mass, radius, position, Vector2.Zero)
        {
        }

        public Star(TimeSpan life, Color color, double mass, double radius, Vector2 position, Vector2 velocity, float maxVelocity = float.PositiveInfinity) 
            : base(color, mass, radius, position, velocity, maxVelocity)
        {
            _life = life;
            Type = StarType.Small;

            
            //Texture = Assets.Get<Texture2D>("Red Planet");
            Animation = new Animation(48, 0.02, 50, 50, 12, 4, 0, 0, 1);
            //Color = Color.White * 0.4f;
            Texture = Assets.Get<Texture2D>("Forming");
            //Animation = new Animation(48, 0.06, 50, 50, 12, 4, 0, 0);
        }

        #endregion

        public enum StarType
        {
            Small,
            Medium,
            Large,
        }

        #region Properties

        public bool IsDead
        {
            get { return _life <= TimeSpan.Zero; }
        }

        public bool AlmostDied { get; set; }

        public bool IsAlmostDead
        {
            // For sound effect.
            get { return _life <= TimeSpan.FromSeconds(2.5); }
        }

        public StarType Type { get; set; }

        #endregion

        #region Update Loop

        public override void Update(GameTime time)
        {
            if (_life > TimeSpan.Zero)
                _life -= time.ElapsedGameTime;

            if (animEnded)
            {
                base.Update(time);
            }
        }
        #endregion

        public override void Draw(SpriteBatch spriteBatch, GameTime time)
        {
            if (IsDead)
            {
                if (IsCompletelyDead || Animation.IsFinished())
                {
                    IsCompletelyDead = true;
                    return;
                }

                if (!_beganExplosion)
                {
                    Animation = new Animation(39, 0.03, 170, 170, 12, 4, 0, 0, 1, true);
                   
                    Texture = Assets.Get<Texture2D>("Explosion");

                    _beganExplosion = true;
                }

                Color = Color.White;
                base.Draw(spriteBatch, time);

                return;
            }

            //base.Draw(spriteBatch, time);
            //TextureOverlayOpacity = 1;
            // Change textures based on radius/mass

            if (Animation.IsFinished())
            {
                animEnded = true;
                Animation = new Animation(60, 0.06, 170, 170, 12, 5, 0, 0);
                Texture = Assets.Get<Texture2D>("Yellow Sun");
            }

            if (animEnded)
            {
                if (Radius > 40)
                {
                    Type = StarType.Large;
                    Texture = Assets.Get<Texture2D>("Blue Sun");
                }
                else if (Radius > 25)
                {
                    Type = StarType.Medium;
                    Texture = Assets.Get<Texture2D>("Red Sun");
                }
                else
                {
                    Type = StarType.Small;
                    Texture = Assets.Get<Texture2D>("Yellow Sun");
                }
            }

            // Animation = new Animation(48, 0.06, 50, 50, 12, 4, 0, 0);

            if (animEnded) Color = Color.Lerp(new Color(0f, 0f, 0f, 1f), new Color(1f, 1f, 1f, 1f), (float)_life.TotalMilliseconds / 20000);
            base.Draw(spriteBatch, time);
        }
    }
}
